package com.hook3.pianke.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hook3.pianke.Adapter.ShopAdapter;
import com.hook3.pianke.Net.Url;
import com.hook3.pianke.activity.base.BaseActiviy;
import com.hook3.pianke.domain.ShopInfo;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * 良品页面
 * Created by HOME on 2015/12/8.
 */
public class ShopActivity {
    private static final String TAG = ShopActivity.class.getSimpleName();
    private Activity activity;
    private Toolbar toolbar;  //标题
    private FrameLayout frameLayout;//实现View;l
    private ListView listView;
    private List<ShopInfo> shopinfo;

    public class Json {
        public Data data;
        public String result;
    }

    public class Data {
        public List<ShopInfo> list;
        public String result;

    }

    public ShopActivity(Activity activity, Toolbar toolbar, FrameLayout frameLayout) {
        this.activity = activity;
        this.toolbar = toolbar;
        this.frameLayout = frameLayout;
        init();
    }

    /**
     * 实例化自己要实现的内容
     */
    public void init() {
        toolbar.setTitle("良品");

        listView = new ListView(activity);
        listView.setVerticalScrollBarEnabled(false);  //隐藏滚动条
        listView.setOnItemClickListener(new MyOnItemClickListener());
        //联网得到数据
        RequestParams params = new RequestParams(Url.Shop);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //解析json
                Gson gson = new Gson();
                Log.e(TAG, result);
                shopinfo = gson.fromJson(result, Json.class).data.list;
                listView.setAdapter(new ShopAdapter(shopinfo, activity));
                frameLayout.addView(listView);
                frameLayout.setTag(listView);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    class MyOnItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(activity, BaseActiviy.class);

            intent.putExtra("contentid", shopinfo.get(position).getContentid());
            activity.startActivity(intent);
        }
    }
}
